5 * Created on Feb 4, 2009
7 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * API module that imports an XML file like Special:Import does
32 class ApiImport
extends ApiBase
{
34 public function execute() {
35 $user = $this->getUser();
36 $params = $this->extractRequestParams();
38 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
41 if ( isset( $params['interwikisource'] ) ) {
42 if ( !$user->isAllowed( 'import' ) ) {
43 $this->dieUsageMsg( 'cantimport' );
45 if ( !isset( $params['interwikipage'] ) ) {
46 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
48 $source = ImportStreamSource
::newFromInterwiki(
49 $params['interwikisource'],
50 $params['interwikipage'],
51 $params['fullhistory'],
56 if ( !$user->isAllowed( 'importupload' ) ) {
57 $this->dieUsageMsg( 'cantimport-upload' );
59 $source = ImportStreamSource
::newFromUpload( 'xml' );
61 if ( !$source->isOK() ) {
62 $this->dieStatus( $source );
65 $importer = new WikiImporter( $source->value
, $this->getConfig() );
66 if ( isset( $params['namespace'] ) ) {
67 $importer->setTargetNamespace( $params['namespace'] );
68 } elseif ( isset( $params['rootpage'] ) ) {
69 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
70 if ( !$statusRootPage->isGood() ) {
71 $this->dieStatus( $statusRootPage );
74 $reporter = new ApiImportReporter(
77 $params['interwikisource'],
82 $importer->doImport();
83 } catch ( Exception
$e ) {
84 $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
87 $resultData = $reporter->getData();
88 $result = $this->getResult();
89 ApiResult
::setIndexedTagName( $resultData, 'page' );
90 $result->addValue( null, $this->getModuleName(), $resultData );
93 public function mustBePosted() {
97 public function isWriteMode() {
101 public function getAllowedParams() {
105 ApiBase
::PARAM_TYPE
=> 'upload',
107 'interwikisource' => array(
108 ApiBase
::PARAM_TYPE
=> $this->getConfig()->get( 'ImportSources' ),
110 'interwikipage' => null,
111 'fullhistory' => false,
112 'templates' => false,
113 'namespace' => array(
114 ApiBase
::PARAM_TYPE
=> 'namespace'
120 public function needsToken() {
124 protected function getExamplesMessages() {
126 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
127 'namespace=100&fullhistory=&token=123ABC'
128 => 'apihelp-import-example-import',
132 public function getHelpUrls() {
133 return 'https://www.mediawiki.org/wiki/API:Import';
138 * Import reporter for the API
141 class ApiImportReporter
extends ImportReporter
{
142 private $mResultArr = array();
145 * @param Title $title
146 * @param Title $origTitle
147 * @param int $revisionCount
148 * @param int $successCount
149 * @param array $pageInfo
152 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
153 // Add a result entry
156 if ( $title === null ) {
157 # Invalid or non-importable title
158 $r['title'] = $pageInfo['title'];
159 $r['invalid'] = true;
161 ApiQueryBase
::addTitleInfo( $r, $title );
162 $r['revisions'] = intval( $successCount );
165 $this->mResultArr
[] = $r;
167 // Piggyback on the parent to do the logging
168 parent
::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
172 return $this->mResultArr
;